﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using DOTNETGROUP.Entities;
using DOTNETGROUP.Data;


public partial class Admin_NguoiDung_QLNguoiDung : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind_Data();
        }
    }

    protected void Bind_Data()
    {
        try
        {
            TList<NguoiDung> Tlist_NguoiDung = DataRepository.NguoiDungProvider.GetAll();
            gridCatalog.DataSource = Tlist_NguoiDung;
            gridCatalog.DataBind();
        }
        catch (Exception)
        {
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {

        string TenTruyCap = Common.StandardString(txtTenTruyCap.Text);
        if (H_CatalogId.Value.Length == 0 && DataRepository.NguoiDungProvider.GetByTenTruyCap(TenTruyCap) != null)
        {
            lblStatus.Text = "Tên truy cập đã tồn tại, xin vui lòng chọn tên khác!";
            return;
        }
        using (NguoiDung entNguoiDung = new NguoiDung())
        {
            entNguoiDung.Id = Convert.ToInt32(H_CatalogId.Value);
            entNguoiDung.HoTen = Common.StandardString(txtHoTen.Text);
            string MatKhau = Common.StandardString(txtMatKhau.Text);
            string MaTruyCap = TenTruyCap + MatKhau;
            entNguoiDung.TenTruyCap = TenTruyCap;
            entNguoiDung.MaTruyCap = Common.EncryptMD5(MaTruyCap);
            entNguoiDung.MatKhau = Common.EncryptMD5(MatKhau);
            entNguoiDung.NgayCapNhat = Common.GetServerDateTime();
            bool updateStatus = false;
            try
            {
                if (entNguoiDung.Id == 0)
                {
                    updateStatus = DataRepository.NguoiDungProvider.Insert(entNguoiDung);
                }
                else
                {
                    entNguoiDung.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.NguoiDungProvider.Update(entNguoiDung);
                }
                if (!updateStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.NGUOIDUNG_CAPNHAT_THATBAI, true);
                }
                else
                {
                    H_CatalogId.Value = "0";
                    txtHoTen.Text = "";
                    txtMatKhau.Text = "";
                    txtTenTruyCap.Text = "";
                    Bind_Data();
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.NGUOIDUNG_CAPNHAT_THANHCONG, true);
                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.NGUOIDUNG_CAPNHAT_THATBAI, true);
            }
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        H_CatalogId.Value = "0";
        txtHoTen.Text = "";
        txtMatKhau.Text = "";
        txtTenTruyCap.Text = "";
        lblStatus.Text = "";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

            LinkButton l = (LinkButton)e.Row.FindControl("cmdDelete");
            l.Attributes.Add("onclick", "javascript:return " + "confirm('Bạn chắc chắn muốn xóa thông tin này?');");
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            try
            {
                int IdNguoiDung = Convert.ToInt32(e.CommandArgument.ToString());
                bool deleteStatus = DataRepository.NguoiDungProvider.Delete(IdNguoiDung);
                if (!deleteStatus)
                {
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.NGUOIDUNG_XOA_THATBAI, true);
                }
                else
                {
                    Bind_Data();
                    H_CatalogId.Value = "0";
                    txtHoTen.Text = "";
                    txtMatKhau.Text = "";
                    txtTenTruyCap.Text = "";
                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THANHCONG, Constants.NGUOIDUNG_XOA_THANHCONG, true);
                }
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.NGUOIDUNG_XOA_THATBAI, true);
                return;
            }
        }
        if (e.CommandName == "Select")
        {
            try
            {
                int IdNguoiDung = Convert.ToInt32(e.CommandArgument.ToString());
                using (NguoiDung entNguoiDung = DataRepository.NguoiDungProvider.GetById(IdNguoiDung))
                {
                    if (entNguoiDung != null)
                    {
                        H_CatalogId.Value = entNguoiDung.Id.ToString();
                        txtHoTen.Text = entNguoiDung.HoTen;
                        txtTenTruyCap.Text = entNguoiDung.TenTruyCap;
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
